/**
 * Created by dongchen on 2016-03-29
 */
define([
    'ViewBase',
    'jquery',
    'lodash',
    'ajaxUtil',
    'js/module/topTab/topTabView',
    'bootbox',
    'moment',
    'bootstrap',
    'bootstrap-table',
    'bootstrap-table-zh-CN'
], function (ViewBase, $, _, ajaxUtil, topTabView, bootbox, moment) {
    var
        tplPath = '/js/module/charge/charge.tpl',
        cssPath = '/js/module/charge/charge.css',
        url_orders = '/admin/orders',
        url_orders_update = '/admin/orders/update',
        url_orders_download = '/admin/orders/download';

    var view = ViewBase.extend({
        getTplById : function (tplId, data){
            return _.template(this.getTpl(tplPath, tplId), {'variable': 'data'})(data);
        },

        events: {
            'click -> .charge-tool-bar .btn-charge-fin': 'onClickChargeFinHandler',
            'click -> .charge-tool-bar .btn-order-download': 'onClickOrderDownloadHandler'
        },

        init: function (opts) {
            view.__super__.init(opts);
            this.loadCss(cssPath);
        },

        update: function () {
        },

        render: function () {
            this.el.html(_.template(this.getTpl(tplPath, "charge-user-ctn-tpl")));
            var topTab = {
                tab : ['未付款', '付款失败', '付款成功', '充值完成']
            };
            new topTabView({pid : 'charge-top-tab-view', el : this.el.find('.charge-top-tab-ctn'), callback : this.topTabCallback()}).render(topTab);
        },

        /**
         * top tab click事件的回调函数
         * @returns {Function}
         */
        topTabCallback : function(){

            var f = this.onTopTabChange;
            var _this = this;

            return function(report){
                f.call(_this, report);
            }
        },

        onTopTabChange : function(tabName){
            switch(tabName){
                case '未付款': {
                    this.renderOrderTable('0', {showCheckBoxCol : true});
                    break;
                }
                case '付款失败' : {
                    this.renderOrderTable('1,3');
                    break;
                }
                case '付款成功' : {
                    this.renderOrderTable('2', {showCheckBoxCol : true, showToolBar : true});
                    break;
                }
                case '充值完成': {
                    this.renderOrderTable('4');
                    break;
                }
            }
        },

        renderOrderTable : function(status, options){
            var _this = this;

            this.getOrdersbyStatus(status).done(function(res){
                var $target = _this.el.find('.charge-info-ctn');
                $target.html(_this.getTplById('charge-info-content-tpl'));

                var data = [];
                _.each(res.data, function(item, index){
                    data.push({
                        seq : index + 1,
                        orderId : item.order_id,
                        mobile : item.mobile,
                        package : item.package,
                        amount : (item.amount / 100).toFixed(2),
                        time : moment(item.update_time).format('YYYY-MM-DD HH:mm:ss')
                    });
                });

                var tableOptions = {
                    columns: [{
                        field: 'seq',
                        title: '序号',
                        sortable : true
                    }, {
                        field: 'orderId',
                        title: '订单号',
                        sortable : true
                    },{
                        field: 'mobile',
                        title: '充值号码'
                    }, {
                        field: 'package',
                        title: '流量包'
                    }, {
                        field: 'amount',
                        title: '金额(元)'
                    }, {
                        field: 'time',
                        title: '充值时间',
                        sortable : true
                    }],
                    data: data,
                    onlyInfoPagination : true,
                    search : true,
                    selectItemName : 'ckeckCol'
                };
                if(options && options.showCheckBoxCol){
                    tableOptions.columns.unshift({
                        field: 'ckeckCol',
                        title: '全选',
                        checkbox : true
                    });
                }
                if(options && options.showToolBar){
                    $target.find('.charge-tool-bar').removeClass('hidden');
                }
                // if(options && options.showToolBar){
                //     $target.find('.charge-tool-bar').removeClass('hidden');
                // }
                var table = $target.find('table').bootstrapTable(tableOptions);
            });
        },

        getOrdersbyStatus : function(status){
            return ajaxUtil.doRequest_v2(url_orders, {status : status});
        },

        onClickChargeFinHandler: function (event) {

            var
                _this = this,
                $table = this.el.find('.charge-info-ctn table');

            var selectRecords = $table.bootstrapTable('getSelections');
            if(selectRecords.length === 0){
                bootbox.alert('没有选中的订单');
                return;
            }

            ajaxUtil.doPostJsonpRequest_v2(url_orders_update, {orders : JSON.stringify(selectRecords)}).done(function(res){
                bootbox.alert('选中订单数: ' + selectRecords.length + '\n更新订单数: ' + res);
                _this.onTopTabChange('付款成功');
            });
        },

        onClickOrderDownloadHandler: function (event) {
            var
                $table = this.el.find('.charge-info-ctn table');

            var selectRecords = $table.bootstrapTable('getSelections');
            if(selectRecords.length === 0){
                bootbox.alert('没有选中的订单');
                return;
            }
            ajaxUtil.download2(url_orders_download, {orders : selectRecords});
        },

        onClickChargeInfoeQueryHandler: function (event) {
            var
                _this = this,
                $target = $(event.target),
                mobile = $target.parent().find('.demo-1-chargeInfo-mobile').val(),
                month = $target.parent().find('.demo-1-chargeInfo-month').val(),
                $respCtn =_this.el.find('.demo-1-chargeInfo-resp'),
                data = {
                    mobile: mobile,
                    month: month
                };
            $respCtn.html('');
            ajaxUtil.doRequest_v2(url_payment, data).done(function (res) {

                var html = _.template(_this.getTpl(tplPath, 'demo-1-chargeInfo-resp-tpl'), {'variable': 'data'})(res.data);
                $respCtn.html(html);
            });
        }
    });
    return view;
});